import javax.print.DocFlavor;
import java.awt.image.BufferedImageFilter;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-05
 * Time: 15:40
 */
public class Sort {

    //插入排序
    //时间复杂度：O（n^2）最坏情况下
    //最好情况下：O(N) 数据有序的情况下
    //结论：当给定的数据趋于有序时，使用直接插入排序
    //空间复杂度：o(1)
    //稳定性：稳定

    public static void insertSort(int[] array){

        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = 0;
            for (j = i - 1; j >= 0 ; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    //array[j + 1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void shellSort(int[] array){
        int gap = array.length / 2;
        while (gap > 0){
            shell(array,gap);
            gap /= 2;
        }
    }

    //希尔排序
    //不稳定
    public static void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (;j >= 0;j -= gap){
                if (array[j] > tmp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    //选择排序
    //不稳定
    //时间复杂度：O(N^2)
    //空间复杂度：O(1)
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int min = i;
            for (int j = i; j < array.length; j++) {
                if (array[j] < array[min]){
                    min = j;
                }
            }
            if (min != i){
                int tmp = array[i];
                array[i] = array[min];
                array[min] = tmp;
            }
        }
    }

    //选择排序2
    public static void selectSort2(int[] array){
        int left = 0;
        int right = array.length-1;

        while (left < right){
            int minIndex = left;
            int maxIndex = left;
            for (int i = left + 1; i <= right ; i++) {
                if (array[i] < array[minIndex]){
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]){
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if (left == maxIndex){
                maxIndex =minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }
    public static void swap(int[] array,int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    /**
     * 堆排序
     * 时间复杂度:O(n*logn)
     * 空间复杂度:O(1)
     * 稳定性 不稳定
     * @param array
     */
    public static void heapSort(int[] array){
        createHeap(array);
        int end = array.length-1;
        while (end > 0){
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }
    public static void createHeap(int[] array){
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            siftDown(array,parent,array.length);
        }
    }
    private static void siftDown(int[] array,int parent,int length){
        int child = 2 * parent+1;
        while (child < length){
            if (child + 1 < length && array[child] < array[child+1]){
                child++;
            }
            if (array[child]>array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    /**
     * 冒泡排序
     * 时间复杂度 O(n^2)
     *          如果是最好的情况下是O(n)
     * 空间负杂度 O(1)
     * 稳定性 稳定
     */
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]){
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (!flg){
                break;
            }
        }
    }

    /**
     * 快速排序
     * 时间复杂度O(n*logn)
     * 最坏情况下:O(n^2)有序或者逆序
     * 空间复杂度O(log)
     * 温度
     */
    public static void quickSort(int[] array){
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start >= end){
            return;
        }
//        System.out.println("start:"+start);
//        System.out.println("end:"+end);

        //三数取中
        int index = midNum(array,start,end);

        int par = partition(array,start,end);

        quick(array,start,par-1);

        quick(array, par+1, end);
    }

    private static int midNum(int[] array, int left, int right) {
        int mid = (left+right)/2;
        if(array[left] < array[right]){
            if (array[mid] < array[right]){
                return left;
            }else if (array[mid] > array[right]){
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] > array[left]){
                return left;
            }else if (array[mid] < array[right]){
                return right;
            }else {
                return mid;
            }
        }
    }

    private static int partitionHoare(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right&& array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    private static int partition(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right&& array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    /**
     * 非递归的快速排序算法
     */
    public static void quickSortNor(int[] array){
        Stack<Integer> stack = new Stack<>();

        int left = 0;
        int right = array.length-1;
        int par = partition(array,left,right);


        //左边有2个元素及其以上
        if(par > left+1){
            stack.push(left);
            stack.push(par-1);
        }
        //右边
        if (par < right-1){
            stack.push(par+1);
            stack.push(right);
        }

        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);

            //左边有2个元素及其以上
            if(par > left+1){
                stack.push(left);
                stack.push(par-1);
            }
            //右边
            if (par < right-1){
                stack.push(par+1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     * 时间复杂度 O(N*logN)
     * 空间复杂度 O(N)
     * 稳定性 稳定
     */
    public static void mergeSort(int[] array){
        mergeSortChild(array,0,array.length-1);
    }

    private static void mergeSortChild(int[] array, int left, int right) {
        if (left >=right){
            return;
        }

        int mid = (left+right)/2;

        mergeSortChild(array,left,mid);

        mergeSortChild(array,mid,right);

        //开始合并
        merge(array,left,mid,right);
    }
    private static void merge(int[] array,int left,int mid,int right){
        int[] tmpArr = new int[right-left+1];
        int k = 0;//tmpArr数组下标

        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        //当2个段落都有数据的时候
        while (s1 <= e1 && s2 <= e2){
            if (array[s1] < array[s2]){
                tmpArr[k] = array[s2];
                k++;
                s1++;
            }else {
                tmpArr[k] = array[s2];
                k++;
                s2++;
            }
        }
        //一个段走完了，把另一个段的数据 拷贝到临时数组
        while(s2 <= e1){
            tmpArr[k++] = array[s1++];
        }
        while (s1 <= e1){
            tmpArr[k++] = array[s1++];
        }
        //临时数组当中存储的是有序的数据 -》 拷贝数据到原始数据数组当中
        for (int i = 0; i < k; i++) {
            array[i+left] = tmpArr[i];
        }
    }

    /**
     * 非递归归并排序
     */
    public static void mergeSortNor(int[] array){
        int gap = 1;
        while (gap < array.length){
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left+gap-1;
                if (mid >= array.length){
                    mid = array.length-1;
                }
                int right = mid+gap;
                if (right >= array.length){
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    /**
     * 计数排序
     * 时间复杂度：O(MAx(N,范围))
     * 空间复杂度: O(范围)
     * 稳定性 : 稳定性
     */
    public static void countSort(int[] array){
        //1.求最大值和最小值
        int max = array[0];
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min){
                min = array[i];
            }
            if (array[i] > max){
                max = array[i];
            }
        }
        //2.定义计数数组 并且求出长度
        int len = max - min + 1;
        int[] countArray = new int[len];
        //计数数组的下标
        //3.遍历原始数组 计数数组开始计数
        for (int i = 0; i < array.length; i++) {
            int index = array[i] - min;
            countArray[index]++;
        }

        //4.遍历计数数组
        int k = 0;//array数组的新下标
        for (int i = 0; i < countArray.length; i++) {
            while (countArray[i] != 0){
                array[k] = i + min;
                k++;
                countArray[i]--;
            }
        }
    }
}
